package top.fzqblog.kotlinapp.adapter

import android.content.Context
import android.support.v4.view.ViewCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_house_detail.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import top.fzqblog.kotlinapp.R
import top.fzqblog.kotlinapp.func.log
import top.fzqblog.kotlinapp.listener.OnItemClickListener
import top.fzqblog.kotlinapp.model.Record

/**
 * Created by 抽离 on 2017/12/19 0019.
 */
class HouseAdapter(var context: Context, var result: List<Record>) : RecyclerView.Adapter<HouseViewHolder>() {

    private var onItemClickListener: OnItemClickListener? = null

    override fun getItemCount(): Int {
        return result.size
    }

    override fun onCreateViewHolder(container: ViewGroup?, position: Int): HouseViewHolder {
        val view: View = LayoutInflater.from(context).inflate(R.layout.item_house, container, false)
        val houseViewHolder = HouseViewHolder(view)
        return houseViewHolder
    }


    override fun onBindViewHolder(viewHolder: HouseViewHolder?, position: Int) {
        val record: Record = result[position]
        Glide.with(context)
                .load(record.houseImg)
                .fitCenter()
                .placeholder(R.mipmap.house_default_pic)
                .into(viewHolder?.houseImageView)
        ViewCompat.setTransitionName(viewHolder?.houseImageView, record.houseId + "")
        with(viewHolder!!) {
            sectionName.text = record.section
            huxing.text = record.room + "室" + record.hall + "厅"
            area.text = record.area?.toString() +"m²"
            floor.text = record.floor + "层/" + record.totalFloor + "层"
            towards.text = record.towards
            decoration.text = record.decoration
            price.text = record.price + "万"
            unitPrice.text = record.unitPrice + "元/m²"
            viewHolder.houseItem.onClick {
                log("点击一项进入详情页-------"+position + "houseId:" + record.houseId)
                onItemClickListener?.onClick(position, record.houseId, record.houseImg, viewHolder.houseImageView)
            }
        }
        if(position == itemCount - 1){
            viewHolder.bottomLine.visibility = View.INVISIBLE
        }else{
            viewHolder.bottomLine.visibility = View.VISIBLE
        }
    }

    fun addAll(list: List<Record>) {
        result += list
    }

    fun setClickListener(onItemClickListener: OnItemClickListener){
        this.onItemClickListener = onItemClickListener
    }

}

private operator fun Any.plus(s: String): String {
    return this.toString() + s
}

class HouseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val houseItem: LinearLayout = itemView.findViewById(R.id.houseItem)

    val houseImageView: ImageView = itemView.findViewById(R.id.houseImageView)
    val sectionName: TextView = itemView.findViewById(R.id.sectionName)
    val huxing: TextView = itemView.findViewById(R.id.huxing)
    val area: TextView = itemView.findViewById(R.id.area)
    val floor: TextView = itemView.findViewById(R.id.floor)
    val towards: TextView = itemView.findViewById(R.id.towards)
    val decoration: TextView = itemView.findViewById(R.id.decoration)
    val price: TextView = itemView.findViewById(R.id.price)
    val unitPrice: TextView = itemView.findViewById(R.id.unitPrice)
    val bottomLine: View = itemView.findViewById(R.id.bottomLine)
}
